<?php

require_once('functions/pageLoad.php');

$page_title = 'Link exchange';

require_once('functions/recaptchalib.php');
$publickey = "6LdLhAYAAAAAABwx-1kiacx5VqTj5YWFnanQE3NS";
$privatekey = "6LdLhAYAAAAAABdiW_7-udhTrdGURvR6XtM9eUTe";

if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form'] == 'links'){

$email_regex = "/^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/i";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

$fail.= (strlen($_POST['users_name']) == 0) ? '<li>Your name was not entered</li>' : '';
$fail.= (preg_match($email_regex, $_POST['users_email']) == 0) ? '<li>Your email address is invalid</li>' : '';
$fail.= (strlen($_POST['web_link']) == 0) ? '<li>Your website link was not entered</li>' : '';
$fail.= (!$resp->is_valid) ? '<li>Incorrect ReCaptcha phrase entered</li>' : '';

if(strlen($fail) > 0){

// PHAIL

$results = '<div class="yellow_box"><p style="font-weight: bold; color: #cc0000;">Please correct the following errors;</p><ul style="font-weight: bold; color: #cc0000;">'.$fail.'</ul></div>';

}

else
{

// FINE - INSERT, SEND EMAIL AND REDIRECT

	require("classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['no_reply_email'];
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($settings['site_email']);

	$mail->Subject = "Link exchange request";
	$mail->Body = "Name: ".$_POST['users_name']."\r\n\r\nEmail: ".$_POST['users_email']."\r\n\r\nWeb link: ".$_POST['web_link']."\r\n\r\nIP Address: ".$_SERVER['REMOTE_ADDR']."\r\n\r\nAdditional info: ".$_POST['info'];

	$mail->Send();

$results = '<p style="font-weight: bold; color: #009900;">Thank you! We have received your link exchange request and will get back to you.</p>';

$_POST['users_name'] = '';
$_POST['users_email'] = '';
$_POST['web_link'] = '';
$_POST['info'] = '';

}

}


include('includes/meta.php');
include('includes/header.php');
include('includes/navigation.php');

?>

<h1>Link exchange</h1>

<?php echo $results; ?>

<div id="error_div" style="display: none;"></div>

<p>We offer link exchanges to websites that have content relevant to our users. Before completing the link exchange request form below, please ensure that you have a reciprocal link visible on your website that is easy for us to find.</p>
<p><strong>Link example</strong></p>
<p>&lt;a href="http://www.iwantatradesman.co.uk" title="I Want A Tradesman"&gt;I Want A Tradesman&lt;/a&gt;<br />&lt;p&gt;Online quotes from local tradesmen. Including; Plumbers, Electricians, Builders, Joiners and many more!&lt;/p&gt;</p>

<form method="post" action="/link-exchange.php" id="links">

<input type="hidden" id="form" name="form" value="links" style="display: none;" />

<div id="registration_form">

<div class="formAreaWrap">

<div class="formRowWrapper" id="users_name_wrap">
<div class="formRowLeft"><label for="users_name">Name:</label></div>
<div class="formRowMiddle"><input type="text" name="users_name" id="users_name" class="textbox" onfocus="return highlight('users_name');" onblur="return removeHighlight('users_name');" value="<?php echo $_POST['users_name']; ?>" /></div>
<div class="formRowRight" id="users_name_result"></div>
</div>

</div><div class="formAreaWrap">

<div class="formRowWrapper" id="users_email_wrap">
<div class="formRowLeft"><label for="users_email">Email:</label></div>
<div class="formRowMiddle"><input type="text" name="users_email" id="users_email" class="textbox" onfocus="return highlight('users_email');" onblur="return removeHighlight('users_email');" value="<?php echo $_POST['users_email']; ?>" /></div>
<div class="formRowRight" id="users_email_result"></div>
</div>

</div><div class="formAreaWrap">

<div class="formRowWrapper" id="web_link_wrap">
<div class="formRowLeft"><label for="web_link">Website link:</label></div>
<div class="formRowMiddle"><input type="text" name="web_link" id="web_link" class="textbox" onfocus="return highlight('web_link');" onblur="return removeHighlight('web_link');" value="<?php echo $_POST['web_link']; ?>" /></div>
<div class="formRowRight" id="web_link_result"></div>
</div>

</div><div class="formAreaWrap">

<div class="formRowWrapper" id="info_wrap">
<div class="formRowLeft"><label for="info">Additional info:</label></div>
<div class="formRowMiddle"><textarea name="info" id="info" class="textbox" onfocus="return highlight('info');" onblur="return removeHighlight('info');" rows="5" cols="30"><?php echo stripslashes($_POST['info']); ?></textarea></div>
<div class="formRowRight" id="info_result"></div>
</div>

</div>

<div class="formRowWrapper">
<div class="formRowLeft">&nbsp;</div>
<div class="formRowMiddle" style="width: 450px; font-size: 16px;"><?php echo recaptcha_get_html($publickey); ?></div>
</div>

<div class="formRowWrapper">
<div class="formRowLeft">&nbsp;</div>
<div class="formRowMiddle">
<script type="text/javascript">document.write('<'+'a onclick="return formval(\'links\');" style="cursor: pointer;"><'+'img src="/images/submit_button.gif" alt="Submit" /><'+'/a>');</script>
<noscript><input type="submit" value="Register" class="blue_submit_button" /></noscript></div>
</div>

</div>
</form>

<?php 

include('includes/rightColumn.php');
include('includes/footer.php');

?>